﻿using EntityModel;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore.Internal;
using System.Linq;

namespace Super_Star.Controllers
{
    /// <summary>
    /// 用户缴费
    /// </summary>
    [Route("api/[controller]")]
    [ApiController]
    public class UserPaymentController : ControllerBase
    {
        private readonly APIDBContext db;

        public UserPaymentController(APIDBContext context)
        {
            db = context;
        }
        /// <summary>
        /// 查找缴费人员信息
        /// </summary>
        /// <param name="StudentName"></param>
        /// <param name="Department"></param>
        /// <param name="Class"></param>
        /// <returns></returns>
        [HttpGet]
        public IQueryable<UserPayment> StudentEntity([FromHeader] string StudentName, [FromHeader] string Department, [FromHeader] string Class)
        {
            IQueryable<EM_Students> query = db.EM_Students;
            string a = "";
            a += "a";
            if (!string.IsNullOrEmpty(StudentName))
            {
                query = query.Where(s => s.StudentName == StudentName);
            }
            if (!string.IsNullOrEmpty(Department))
            {
                query = query.Where(s => s.Department == Department);
            }
            if (!string.IsNullOrEmpty(Class))
            {
                query = query.Where(s => s.Class == Class);
            }
            return (IQueryable<UserPayment>)query;
        }

        /// <summary>
        /// 添加缴费人员信息
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        [HttpPost("UpUser")]
        public bool UpUser(Guid  Id)
        {
            UserPayment? userPayment = db.UserPayment.Where(s => s.Id == Id).SingleOrDefault();
            if (userPayment != null)
            {
                db.UserPayment.Update(userPayment);
                int result = db.SaveChanges();
                return result > 0;
            }
            return false;
        }

        /// <summary>
        /// 删除缴费人员信息
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        [HttpDelete("DelUser")]
        public bool DelUser(Guid Id)
        {
            UserPayment? userPayment = db.UserPayment.Where(s => s.Id == Id).SingleOrDefault();
            if (userPayment != null)
            {
                db.UserPayment.Remove(userPayment);
                int result = db.SaveChanges();
                return result > 0;
            }
            return false;
        }
    }
}
